//
//  BaseViewController.m
//  ManStoreDemo
//
//  Created by qianfeng01 on 15/7/22.
//  Copyright (c) 2015年 xiangmu. All rights reserved.
//

#import "BaseViewController.h"
#import "SingleViewController.h"
#import "JHRefresh.h"
@interface BaseViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>

@end

@implementation BaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    self.dataArray=[[NSMutableArray alloc] init];
    [self setCollectionView];
}
-(void)setCollectionView{
    _flowLayout=[[UICollectionViewFlowLayout alloc] init];
    [_flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
    _flowLayout.minimumInteritemSpacing=0;
    _flowLayout.minimumLineSpacing=10;
    self.automaticallyAdjustsScrollViewInsets=NO;
    _collectionView=[[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kScreenSize.width, kScreenSize.height-64-49) collectionViewLayout:_flowLayout];
    _collectionView.delegate=self;
    _collectionView.dataSource=self;
    [_collectionView registerNib:[UINib nibWithNibName:@"PictureCell" bundle:nil] forCellWithReuseIdentifier:@"PictureCell"];
    _collectionView.backgroundColor=[UIColor grayColor];
    [self.view addSubview:_collectionView];
    
}
- (void)creatRefreshViewWith:(NSInteger)number {
       __weak typeof (self) weakSelf = self;//弱引用
    
    [_collectionView addRefreshHeaderViewWithAniViewClass:[JHRefreshCommonAniView class] beginRefresh:^{
        if (weakSelf.isRefreshing) {
            return ;
        }
        weakSelf.isRefreshing = YES;//标记正在刷新
        weakSelf.currentPage = 1;
        
        [weakSelf addTaskWithpage:weakSelf.currentPage Number:number isRefresh:YES];
        
    }];
    
    [_collectionView addRefreshFooterViewWithAniViewClass:[JHRefreshCommonAniView class] beginRefresh:^{
        if (weakSelf.isLoadMore) {
            return ;
        }
        weakSelf.isLoadMore = YES;//标记正在刷新
        weakSelf.currentPage ++;//页码加1
        
       [weakSelf addTaskWithpage:weakSelf.currentPage Number:number isRefresh:YES];
    }];
    
}
-(void)firstLoadWith:(NSInteger)number{
    self.currentPage=1;
    [self addTaskWithpage:self.currentPage Number:number isRefresh:NO];
}
- (void)addTaskWithpage:(NSInteger)page Number:(NSInteger)number isRefresh:(BOOL)isRefresh{
        NSString *url=[NSString stringWithFormat:kUrl,number,page];
    _manager=[[AFHTTPRequestOperationManager alloc] init];
    _manager.responseSerializer=[AFHTTPResponseSerializer serializer];
    __weak typeof(self) weakSelf=self;
    [_manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"下载成功");
        if (responseObject) {
            if (weakSelf.currentPage == 1) {
                [weakSelf.dataArray removeAllObjects];
            }
            NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
            NSArray *arr=dict[@"data"];
            for (NSDictionary *dic in arr) {
                BaseModel *model=[[BaseModel alloc] init];
                [model setValuesForKeysWithDictionary:dic];
                [weakSelf.dataArray addObject:model];
            }
            [_collectionView reloadData];
            [weakSelf endRefreshing];
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"下载失败");
    }];
}
- (void)endRefreshing {
    if (self.isRefreshing) {
        self.isRefreshing = NO;//标记刷新结束
        //正在刷新 就结束刷新
        [_collectionView headerEndRefreshingWithResult:JHRefreshResultNone];
    }
    if (self.isLoadMore) {
        self.isLoadMore = NO;
        [_collectionView footerEndRefreshing];
    }
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArray.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    PictureCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"PictureCell" forIndexPath:indexPath];
    cell.layer.borderColor=[UIColor blackColor].CGColor;
    cell.layer.borderWidth=2;
    BaseModel *model=self.dataArray[indexPath.row];
    [cell showDataWithModel:model];
    return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake((kScreenSize.width-30)/2, 230);
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(0, 10, 10, 10);
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
     BaseModel *model=self.dataArray[indexPath.row];
    DetailViewController *controller=[[DetailViewController alloc] init];
    controller.item_id=model._id;
   controller.hidesBottomBarWhenPushed=YES;
    [self.navigationController pushViewController:controller animated:YES];
}

@end
